<?
/**
 * PHP file upload handler.
 * Copyright Thin File (Pvt) Ltd. 2005.
 * http://upload.thinfile.com
 */
?>
<html>
<head>
<title>Thin File Upload</title>
</head>
<body  bgcolor="FFFFFF">
<table border="0" cellpadding="5" width="100%" align="center" bgcolor="#F0F0FF">
  <!--<tr><td colspan="2" bgcolor="#6699CC" align="center"><font color="#FFFFFF" size="+1" align="center">Files Uploaded</font></td>
 
</tr>-->
  <?


include "../global.php";
include_once("../MVC_Core/MVC_ORM.php");
include_once("../MVC_Core/MVC_SQL.php");
include_once("../MVC_Core/MVC_Model.php");
include_once("../MVC_Core/MVC_View.php");
include_once("../MVC_Core/MVC_Read_ini.php");
include_once("../function/functions.php");
include_once("../function/class.json.php");
include_once("../function/file.php");
include_once("../ini/language/".FOLDER_LANGUAGE."format.php");
//echo PATH_UPLOAD_PRODUCT;
include_once(PATH_SQL . 'product_sql.php');
include_once(PATH_MODEL . 'product_model.php');
//include_once(PATH_VIEW_ADMIN . 'product_view.php');
include_once(PATH_INCLUDE."thumb.php");
include_once(PATH_INCLUDE."constant.php");
$oModelProduct=new Model_Product();
$oDb_Product = new Db_product();
/*
 * SET THE SAVE PATH by editing the line below. Make sure that the path
 * name ends with the correct file system path separator ('/' in linux and
 * '\\' in windows servers (eg "c:\\temp\\uploads\\" )
 */

//$save_path_temp='Temp\\';    
$save_path='..\\..\\upload\\product_image\\';
$save_path2='../../upload/product_image/';
//$save_path=PATH_UPLOAD_PRODUCT;
$get_id_product=$_REQUEST['sID'];
$get_xid_product=$_REQUEST['xID'];
$file = $_FILES['userfile'];
$k = count($file['name']);
$status=0;
//echo 'qqqqqqqqqqqqqq';

for($i=0 ; $i < $k ; $i++)
{
	if($i %2)
	{
		echo '<tr bgcolor="#FAFAFA"> ';
	}
	else
	{	
		echo '<tr>';
	}
	
	if(isset($save_path) && $save_path!="")
	{
		$status=1;
		$name = split('/',substr($file['name'][$i],strrpos($file['name'][$i],"%2")+ 2,strlen($file['name'][$i])));
		//$name = str_replace("%", "", $name);
		$name = substr($file['name'][$i],strrpos($file['name'][$i],"%2")+ 2,strlen($file['name'][$i]));
		//move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
		//$save_path=""; %2
		/** edit 2708 */
		$oUpload = new Upload($_FILES);
		$productID=$get_id_product;
		if(strlen($productID)==2){
			$imageID=date("Y").date("m").date("d").ord(substr($productID,0,1)). "0" .substr($productID,1,1);
		}
		else{
			$imageID=date("Y").date("m").date("d").ord(substr($productID,0,1)).substr($productID,1,2);
		}
		//if ($oUpload->isAllowImageFile("userfile")) {
			//echo $file['name'][$i];
			$ext = substr($file['name'][$i],strrpos($file['name'][$i],".")+ 1,strlen($file['name'][$i]));
			//echo $ext;
		//}
		$fileUpload = $imageID .$get_xid_product.'.'.$ext ;
		/**************************/
		if( file_exists($save_path.$fileUpload)){
			unlink($save_path.$fileUpload);
		}
		if ($oUpload->isAllowImageFile_OBJ($file['name'][$i])) {
			if ($oUpload->saveAs_OBJ($fileUpload, $save_path2 ,$file['tmp_name'][$i],$file['size'][$i], "over", 0777)) 
			{
				//$_REQUEST_2['product_picture'.$i]=$fileUpload;
				$thumb=new thumbnail_1(PATH_UPLOAD_PRODUCT.$fileUpload);	// generate image_file, set filename to resize
				
				 /* Set thumb 300x200*/
				 //die("ggggggggggg");
				$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH_300);
				$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT_200);
				$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO_300);
				$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
				$thumb->save(PATH_UPLOAD_PRODUCT."thumb300x200/".$fileUpload);
				
				 /* Set thumb 160x120*/
				 
				$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
				$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
				$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
				$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
				$thumb->save(PATH_UPLOAD_PRODUCT."thumbnails/".$fileUpload);
				
				if($oldProduct['product_picture'.$i] !="" && file_exists(PATH_UPLOAD_PRODUCT.$oldProduct['product_picture'.$i])){
					unlink(PATH_UPLOAD_PRODUCT.$oldProduct['product_picture'.$i]);
					unlink(PATH_UPLOAD_PRODUCT."thumbnails/".$oldProduct['product_picture'.$i]);
				}
			}
		/**************************/
		$_REQUEST['product_picture'.$get_xid_product]=$fileUpload;
		$_REQUEST['id']=$get_id_product;
                $result_edit=$oModelProduct->updateProduct_OBJ($_REQUEST);
		}
		
		//echo $result_edit;
		
		$rs_new=$oDb_Product->getImageProduct_OBJ($get_id_product,$get_xid_product);
		if($rs_new){
			foreach($rs_new as $row)
			{
				$row_path=$row['product_picture'.$get_xid_product];
				//echo $save_path.$row_path;
				if((!$row_path) || ($row_path=='')){
					$row_path='no_image.gif';
				}
			}
		}else{
			$row_path='no_image.gif';
		}
		
		
	
		 /*********/
		
	}                                       
	if(! isset($file['name'][$i]) || $file['name'][$i] ==""){                            
		//echo '<tr><td width= "289" height= "309"><img width= "289" height= "309" src= "temp/elephant2.jpg" /> </td></trord(>';
	}else{ 
		$t_temp= rand(1,100) ;
		$str_t="   ..";
		for($i=0 ; $i < $t_temp ; $i++)
		{
			$str_t .=".";
		}
		//echo $save_path2;
		//echo $row_path;
		$row_path_temp=substr($file['name'][$i],strrpos($file['name'][$i],"%2")+ 2,strlen($file['name'][$i]));
		echo '<tr><td align="center" width= "270" height= "239"><img align="center" width= "270" height= "239" src= "'.$save_path2.$row_path.$str_t.'" /> </td></tr>';
	}
	
}

if(! isset($save_path) || $save_path =="")
{
	echo '<tr style="color: #0066cc"  bgcolor="#FCFCFC" ><td colspan=2 align="left">Files have been uploaded but not saved because the destination folder has not been set. Please change the $save_path in upload.php</td></tr>';
	
}

if(isset($userfile_parent))
{
	
	//echo "<tr bgcolor='#FCFCFC' style='color: #0066cc'><td colspan=2>Top level folder hint : $userfile_parent</td></tr>";
}
if($status==0){     
		$rs_defaul=$oDb_Product->getImageProduct_OBJ($get_id_product,$get_xid_product);
		if($rs_defaul){
			foreach($rs_defaul as $row)
			{
				$row_path_defaul=$row['product_picture'.$get_xid_product];
				if((!$row_path_defaul) || ($row_path_defaul=='')){
					$row_path_defaul='no_image.gif';
				}
			}
		}else{
			$row_path_defaul='no_image.gif';
		}
		
			//echo $row_path_defaul;
		
		$t_temp= rand(1,100) ;
		$str_t2="  ...";
		for($i=0 ; $i < $t_temp ; $i++)
		{
			$str_t2 .=".";
		}	
		echo '<tr><td width= "250" height= "200" align="center"><img align="center" width= "250" height= "200" src= "'.$save_path2.$row_path_defaul.$str_t2.'" /> </td></tr>';
	}
?>
</table>
<p>&nbsp;</p>
<!--<p style="text-align:center;">PHP Upload handler provided by
 <a href="http://www.thinfile.com/?tiu">Thin File (Pvt) Ltd.</a></p>
 <p>&nbsp;</p> -->
</body>
</html>
